package com.flexdms.i18n;

import java.sql.SQLException;
import java.util.Locale;

public class DBMsgs extends DefaultResourceImpl
{

	
	
	@Override
	public String getColumnName()
	{
		return I18NMessage.getDatabaseI18nMessage().getDefaultLocale();
	}

	@Override
	protected Object handleGetObject(String key)
	{
		if (isNumeric(key))
		{
			return key;
		}
		
		if (msgs.containsKey(key.toLowerCase()))
		{
			String valueString=msgs.get(key.toLowerCase());
			if (valueString==null)
			{
				//no real entries
				return key;
			} else {
				return valueString;
			}
		}
		
		try
		{
			if (msgs.containsKey(key.toLowerCase()))
			{
				I18NMessage.getDatabaseI18nMessage().updateMsg(key, key, getColumnName());
			} else {
				I18NMessage.getDatabaseI18nMessage().insertMsg(key, key, getColumnName());
			}
			
			msgs.put(key.toLowerCase(), key);
		} catch (SQLException e)
		{
			//ignore. To make the system robust
		}
		
		return key;
	}

	/**
	 * Always contains the key
	 */
	@Override
	public boolean containsKey(String key)
	{
		return true;
	}

	Locale defaultLocale=null;
	public DBMsgs()
	{
		super();
		defaultLocale=I18NMessage.stringToLocale(I18NMessage.getDatabaseI18nMessage().getDefaultLocale());
	}

	@Override
	public Locale getLocale()
	{
		return defaultLocale;
	}
	
	
	

}
